package servlet.soft;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import dao.SoftDao;
import dbutil.DBHelper;

public class EditSoftServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4471290799032187995L;

	public EditSoftServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String softName = request.getParameter("softName");
		String numLic = request.getParameter("numLicense");
		String kmsServer = request.getParameter("kmsServer");
		String keySeg = request.getParameter("keySeg");
		String status = request.getParameter("status");
		
		JSONObject jsonObject = new JSONObject();
		SoftDao softDao = null;
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);
			
			softDao = new SoftDao();
			if(softDao.editSoft(numLic, kmsServer, keySeg, status, softName)) {
				jsonObject.put("retCode", "1");
				conn.commit();
			} else {//修改失败
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "EditSoftServlet dao");
				conn.rollback();
			}
			
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "EditSoftServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}		
	}

	public void init() throws ServletException {
	}

}
